package smedi.ismedi.web.bff.controller.login;

import com.smedi.ismedi.authority.starter.response.AuthorityAccessResponse;
import com.smedi.ismedi.authority.starter.util.AuthorityUtil;
import com.smedi.ismedi.authoritymanagement.api.dto.request.LoginRequest;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteClientLoginFeignApi;
import com.smedi.ismedi.authoritymanagement.api.dto.response.NodeInfoResponse;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import smedi.ismedi.web.bff.infrastructure.constant.CustomError;

/**
 * 用户登陆API
 *
 * @author xulinjun
 */
@RequestMapping("/webbff/v1/login")
@Api(tags = "用户登陆API")
@RestController
@Slf4j
@AllArgsConstructor
public class LoginController {
    private final RemoteClientLoginFeignApi remoteClientLoginFeignApi;

    private final AuthorityUtil authorityUtil;

    /**
     * pc端用户登录
     *
     * @param loginRequest
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "pc端用户登录")
    @PostMapping(value = "/client/pc")
    public ResponseData<AuthorityAccessResponse> postLoginPc(@Validated @RequestBody LoginRequest loginRequest) {
        return remoteClientLoginFeignApi.postLoginPcFast(loginRequest);
    }

    /**
     * PC端用户登出
     *
     * @return
     */
    @ApiOperation(httpMethod = "PUT", value = "pc端用户登出")
    @PutMapping(value = "/client/pc/logout")
    public ResponseData<Boolean> putLogout() {
        String token = authorityUtil.getAccessToken();
        if (!StringUtils.isEmpty(token)) {
            return remoteClientLoginFeignApi.putLogout(token);
        } else {
            throw LogicException.raise(CustomError.TOKEN_MISS);
        }
    }

    /**
     * 获取初始的用户三级组织切换信息
     *
     * @return NodeInfoResponse
     */
    @ApiOperation(httpMethod = "GET", value = "获取初始的用户三级组织切换信息")
    @GetMapping(value = "/getInitScopeTopPermissionInfo")
    public ResponseData<NodeInfoResponse> getInitScopeTopPermissionInfo(){
        return remoteClientLoginFeignApi.getInitScopeTopPermissionInfo("pc");
    }

    /**
     * 获取当前的用户三级组织切换信息
     *
     * @return NodeInfoResponse
     */
    @ApiOperation(httpMethod = "GET", value = "获取当前的用户三级组织切换信息")
    @GetMapping(value = "/getCurrentScopeTopPermissionInfo")
    ResponseData<NodeInfoResponse> getCurrentScopeTopPermissionInfo(){
        return remoteClientLoginFeignApi.getCurrentScopeTopPermissionInfo();
    }
}